﻿using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    public class VectorTool
    {
        public static Vector3 ToV3(Rect r)
        {
            Vector3 v3 = new Vector3(r.x + r.width / 2, r.y + r.height / 2, 0);
            return v3;
        }

        public static Vector2 ToV2(Rect r)
        {
            Vector2 v2 = new Vector3(r.x + r.width / 2, r.y + r.height / 2);
            return v2;
        }

        public static Vector2 ToV2(Vector3 r)
        {
            Vector2 v2 = new Vector2(r.x, r.y);
            return v2;
        }

        public static Vector3 Sub(Vector3 v3, float val)
        {
            v3.x -= val;
            v3.y -= val;
            v3.z -= val;
            return v3;
        }

        public static Vector2 Sub(Vector2 v2, float val)
        {
            v2.x -= val;
            v2.y -= val;
            return v2;
        }

        public static Vector3 Add(Vector3 v3, float val)
        {
            v3.x += val;
            v3.y += val;
            v3.z += val;
            return v3;
        }

        public static Vector2 Add(Vector2 v2, float val)
        {
            v2.x += val;
            v2.y += val;
            return v2;
        }

        public static Vector3 Multiply(Vector3 a,Vector3 b)
        {
            a.x = a.x * b.x;
            a.y = a.y * b.y;
            a.z = a.z * b.z;
            return a;
        }


        public static Vector3[] GetVector3Ary(Transform[] tf, Space space = Space.Self)
        {
            Vector3[] v3 = new Vector3[tf.Length];
            for (int i = 0; i < tf.Length; i++)
            {
                if (space == Space.World)
                {
                    v3[i] = tf[i].position;
                }
                else
                {
                    v3[i] = tf[i].localPosition;
                }
            }

            return v3;
        }
        
        public static float IgnoreYDistance(Vector3 a,Vector3 b)
        {
            a.y = 0;
            b.y = 0;
            return Vector3.Distance(a, b);
        }
        
        public static  float IgnoreZDistance(Vector3 a,Vector3 b)
        {
            a.z = 0;
            b.z = 0;
            return Vector3.Distance(a, b);
        }
        
        public static float CalVectorLen(List<Vector3> points)
        {
            float f = 0;
            for (int i = 0; i < points.Count - 1; i++)
            {
                f += Vector3.Distance(points[i], points[i + 1]);
            }

            return f;
        }


        /// <summary>
        /// 把向量投影到平面上,求出夹角
        /// </summary>
        public static float ProjectPlaneAngle(Vector3 dirA,Vector3 dirB,Vector3 normal)
        {
            dirA = Vector3.ProjectOnPlane(dirA, normal);
            dirB = Vector3.ProjectOnPlane(dirB, normal);
            float inclineAngle = Vector3.Angle(dirB, dirA);
            return inclineAngle;
        }
            
            
            
    }
}